package com.huayue.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.huayue.entity.RoomStatusType;
import com.huayue.util.PageHelper;
import com.mysql.DBHelper;

/**
 * 房态类型
 */
@Component
public class RoomStatusTypeDao implements BaseDao<RoomStatusType> {

    @Override
    public int insert(RoomStatusType entity) {
        return DBHelper.executeUpdate("insert into room_status_type(name,is_deleted) values(?,?)", entity.getName(), 0);
    }

    @Override
    public int update(RoomStatusType entity) {
        return DBHelper.executeUpdate("update room_status_type set name=? where id=?", entity.getName(),
            entity.getId());
    }

    @Override
    public int delete(RoomStatusType entity) {
        return DBHelper.executeUpdate("update room_status_type set is_deleted=1 where id=?", entity.getId());
    }

    @Override
    public List<Map<String, Object>> findAll(Object... obj) {
        return DBHelper.executeQuery("select * from room_status_type");
    }

    @Override
    public List<Map<String, Object>> findPage(PageHelper page, Object... obj) {
        // 传进来一个Object对象,将其强转为所需对象
        RoomStatusType sta = (RoomStatusType)obj[0];
        String sql = "select * from room_status_type where is_deleted=0 ";
        if (obj[0] != null && !"".equals(sta.getName())) {
            sql += " and name like '%" + sta.getName() + "%'";
        }
        return DBHelper.executeQuery(sql + " limit ?,? ", page.getBegin(), page.getPageSize());

    }

    @Override
    public Map<String, Object> findOne(RoomStatusType entity) {
        return DBHelper
            .executeQuery("select * from room_status_type where 1=1 and is_deleted=0 and id=? ", entity.getId()).get(0);
    }

    @Override
    public int count(Object... obj) {
        String sql = "select count(*) counts from room_status_type where is_deleted=0 ";
        // 传进来一个Object对象,将其强转为所需对象
        RoomStatusType sta = (RoomStatusType)obj[0];
        if (obj[0] != null && !"".equals(sta.getName())) {
            sql += " and name like '%" + sta.getName() + "%'";
        }

        return Integer.valueOf(DBHelper.executeQuery(sql).get(0).get("counts").toString());
    }
}
